#include "base/cmd_flags.h"
#include "gtest/gtest.h"

DEFINE_int32(a, 100, "flag a");

TEST(CmdFlags, ParseInt32) {
  char flags1[256];
  strncpy(flags1, "program", sizeof(flags1));
  char flags2[256];
  char* argv[] = {flags1, flags2};
  strncpy(flags2, "a=1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ(100, FLAGS_a);
  strncpy(flags2, "-a=1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ(100, FLAGS_a);
  strncpy(flags2, "--a=xx1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ(100, FLAGS_a);
  strncpy(flags2, "--a=1xx", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ(100, FLAGS_a);
  strncpy(flags2, "--a=1", sizeof(flags2));
  EXPECT_TRUE(ParseFlags(2, argv));
  EXPECT_EQ(1, FLAGS_a);
}

DEFINE_double(c, 0.5, "flag c");

TEST(CmdFlags, ParseDouble) {
  char flags1[256];
  strncpy(flags1, "program", sizeof(flags1));
  char flags2[256];
  char* argv[] = {flags1, flags2};
  strncpy(flags2, "c=1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ(0.5, FLAGS_c);
  strncpy(flags2, "-c=1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ(0.5, FLAGS_c);
  strncpy(flags2, "--c=xx1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ(0.5, FLAGS_c);
  strncpy(flags2, "--c=1xx", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ(0.5, FLAGS_c);
  strncpy(flags2, "--c=0.25", sizeof(flags2));
  EXPECT_TRUE(ParseFlags(2, argv));
  EXPECT_EQ(0.25, FLAGS_c);
}
DEFINE_bool(d, false, "flag d");

TEST(CmdFlags, ParseBool) {
  char flags1[256];
  strncpy(flags1, "program", sizeof(flags1));
  char flags2[256];
  char* argv[] = {flags1, flags2};
  strncpy(flags2, "d=1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_FALSE(FLAGS_d);
  strncpy(flags2, "-d=1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_FALSE(FLAGS_d);
  strncpy(flags2, "--d=xx1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_FALSE(FLAGS_d);
  strncpy(flags2, "--d=1xx", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_FALSE(FLAGS_d);
  strncpy(flags2, "--d=true", sizeof(flags2));
  EXPECT_TRUE(ParseFlags(2, argv));
  EXPECT_TRUE(FLAGS_d);
  strncpy(flags2, "--d=false", sizeof(flags2));
  EXPECT_TRUE(ParseFlags(2, argv));
  EXPECT_FALSE(FLAGS_d);
}

DEFINE_string(e, "abc", "flag e");

TEST(CmdFlags, ParseString) {
  char flags1[256];
  strncpy(flags1, "program", sizeof(flags1));
  char flags2[256];
  char* argv[] = {flags1, flags2};
  strncpy(flags2, "e=1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ("abc", FLAGS_e);
  strncpy(flags2, "-e=1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
  EXPECT_EQ("abc", FLAGS_e);
  strncpy(flags2, "--e=xx1", sizeof(flags2));
  EXPECT_TRUE(ParseFlags(2, argv));
  EXPECT_EQ("xx1", FLAGS_e);
  strncpy(flags2, "--e=", sizeof(flags2));
  EXPECT_TRUE(ParseFlags(2, argv));
  EXPECT_EQ("", FLAGS_e);
}

TEST(CmdFlags, ParseFlagInexist) {
  char flags1[256];
  strncpy(flags1, "program", sizeof(flags1));
  char flags2[256];
  char* argv[] = {flags1, flags2};
  strncpy(flags2, "--f=1", sizeof(flags2));
  EXPECT_FALSE(ParseFlags(2, argv));
}
